﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using B303Langf.Parser;
using B303Langf.Scanner;

namespace B303Langf.ASTVisualizer
{
   /// <summary>
   /// Interaction logic for MainWindow.xaml
   /// </summary>
   public partial class MainWindow : Window
   {
      public MainWindow()
      {
         InitializeComponent();
      }

      private void OnLoadClick( object sender, RoutedEventArgs e )
      {
         var dialog = new Microsoft.Win32.OpenFileDialog();
         dialog.DefaultExt = ".303";
         dialog.Filter = "B303Langf source file (.303)|*.303";

         if( dialog.ShowDialog( this ) == true )
         {
            try
            {
               using( var stream = File.OpenRead( dialog.FileName ) )
               using( var scanner = new Scanner.Scanner( stream ) )
               {
                  var parser = new ASTParser( scanner );
                  var program = parser.ParseProgram();
                  ASTTreeView.DataContext = program;
               }
            }
            catch( Exception ex )
            {
               MessageBox.Show( ex.Message, ex.GetType().Name, MessageBoxButton.OK, MessageBoxImage.Error );
            }
         }
      }

      private void OnCompileClick( object sender, RoutedEventArgs e )
      {
         var dialog = new Microsoft.Win32.OpenFileDialog();
         dialog.DefaultExt = ".303";
         dialog.Filter = "B303Langf source file (.303)|*.303";

         if( dialog.ShowDialog( this ) == true )
         {
            try
            {
               using( var stream = File.OpenRead( dialog.FileName ) )
               using( var scanner = new Scanner.Scanner( stream ) )
               {
                  var parser = new ASTParser( scanner );
                  var program = parser.ParseProgram();

                  var checker = new B303Langf.Parser.ContextualAnalysis.Checker();
                  checker.Check( program );

                  var compiler = new B303Langf.Parser.CodeGenerator.Encoder();
                  compiler.Encode( program );

                  var newFile = File.Create( "obj.tam" );
                  compiler.Save( newFile );

                  ASTTreeView.DataContext = program;
               }
            }
            catch( Exception ex )
            {
               MessageBox.Show( ex.Message, ex.GetType().Name, MessageBoxButton.OK, MessageBoxImage.Error );
            }
         }
      }

      private void OnExitClick( object sender, RoutedEventArgs e )
      {
         Application.Current.Shutdown();
      }
   }
}
